package com.mnb.base.service.impl;

import com.mnb.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.mnb.base.entity.SysMenu;
import com.mnb.base.mapper.SysMenuMapper;
import com.mnb.base.service.SysMenuService;

import java.util.*;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public R insert(SysMenu record) {
        sysMenuMapper.insert(record);
        return R.ok();
    }


    @Override
    public List<SysMenu> selectMenuList(SysMenu sysMenu) {
        List<SysMenu> menuList = sysMenuMapper.selectMenuList(sysMenu);
        return menuList;
    }

    @Override
    public List<Long> selectMenuListByRoleId(Long roleId) {
        List<Long> list = sysMenuMapper.selectMenuListByRoleId(roleId);
        return list;
    }

    @Override
    public SysMenu selectMenuById(Long menuId) {
        return sysMenuMapper.selectMenuById(menuId);
    }

    @Override
    public R udpate(SysMenu sysMenu) {
        sysMenuMapper.updateByPrimaryKey(sysMenu);
        return R.ok();
    }

    @Override
    public R deleteByMenuId(Long menuId) {
        sysMenuMapper.deleteByPrimaryKey(menuId);
        return R.ok();
    }

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectMenuPermsByUserId(Object userId) {
        List<String> perms = sysMenuMapper.selectMenuPermsByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms)
        {
            if (StringUtils.isNotEmpty(perm))
            {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

}
